import { Mesh, MeshStandardMaterial } from "three";
import { STLLoader } from "three/examples/jsm/loaders/STLLoader.js";
import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader.js";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";

export class objLoader {

    static Loader = [
        STLLoader,
        OBJLoader,
        GLTFLoader,
    ]

    private $allBaseObject: any[] = []; // 返回所有基础模型

    load(url: string, type: number, callback: () => void) {
        const cls = objLoader.Loader[type];
        const loader = new cls();
        console.log("模型加载中。。。");
        loader.load(
            url,
            (geometry: any) => {
                const material = new MeshStandardMaterial({
                    color: "rgb(255, 172, 3)", // 设置材质的颜色
                    metalness: 0.5, // 金属度 （1 最像金属，0 最不想金属）
                    roughness: 0, // 粗糙度（0 最光滑，1 最粗糙）
                });
                const model = new Mesh(geometry, material);
                model.castShadow = true;
                model.receiveShadow = true;
                this.$allBaseObject.push(model);
                console.log("模型加载完成!!!");
                callback();
            },
            undefined,
            (error) => {
                console.error(error);
            }
        );

    }
    get allBaseObject() {
        return this.$allBaseObject;
    }
}